﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;
using Microsoft.Office.Interop.Word;

public partial class Auditors_Pagefour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string company = Request.QueryString["CompanyID"];
            string orderid = Request.QueryString["orderid"];
            var v = new CompanyBLL().GetCompanyByCompanyid(company);

            List<DetailTypesEntity> DetailTypesList = new AuditorsBLL().GetDetailTypes();
            List<CompanyDetailEntity> CompanyDetailList = new AuditorsBLL().GetCompanyDetail(company, orderid);
            //  List<CompanyDetailEntity> CompanyDetailListOld = new AuditorsBLL().GetCompanyDetail(company, orderid);

            int Revenue = new AuditorsBLL().GetRevenue(company, orderid);
            //int RevenueOld = new AuditorsBLL().GetRevenue(company, (Convert.ToInt32(orderid) - 1).ToString());

            int cost = new AuditorsBLL().GetCost(company, orderid);
            //int costOld = new AuditorsBLL().GetCost(company, (Convert.ToInt32(orderid) - 1).ToString());

            double ProfitTax = new AuditorsBLL().GetProfit(company, orderid) * 0.165;
            //double ProfitTaxOld = new AuditorsBLL().GetProfitTax(company, (Convert.ToInt32(orderid) - 1).ToString());

            double BankCharge = new AuditorsBLL().GetBankCharge(company, orderid);
            double BankInterestIncome = new AuditorsBLL().GetBankInterestIncome(company, orderid);

            int sum = Revenue - cost;
            //int sumold = RevenueOld - costOld;


            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<div>{0}</div>", v.EnglishName);
            sb.AppendFormat("<div>INCOME STATEMENT</div>");
            sb.AppendFormat("<div>FOR THE orderid ENDED 31st DECEMBER, {0}.</div>", orderid);
            sb.AppendFormat("<div>(Expressed in Hong Kong dollars)</div>");
            sb.AppendFormat("<table>");
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <th>");
            sb.AppendFormat("       </th>");
            sb.AppendFormat("       <th>");
            sb.AppendFormat("           Note");
            sb.AppendFormat("       </th>");
            sb.AppendFormat("       <th>");
            sb.AppendFormat("           {0}", new CompanyBLL().GetCompanyAuditorsEntityByOrderId(orderid).EndTime.Year);
            sb.AppendFormat("       </th>");
            //sb.AppendFormat("       <th>");
            //sb.AppendFormat("           {0}", Convert.ToInt32(orderid) - 1);
            //sb.AppendFormat("       </th>");
            sb.AppendFormat("   </tr>");
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Sales");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", Revenue);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", RevenueOld);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Purchase");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", cost);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", costOld);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Gross Profit");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", sum);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sb.AppendFormat("<tr><td><br /></td></tr>");
            int n = 0;
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Other income");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "1" && fun.Kinds == "9"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    sb.AppendFormat("   <tr>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", Detail.EnglishName);
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount.ToString() : "");
                    sb.AppendFormat("       </td>");
                    //sb.AppendFormat("       <td>");
                    //sb.AppendFormat("           {0}", CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount : "");
                    //sb.AppendFormat("       </td>");
                    sb.AppendFormat("   </tr>");

                    n += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                               ? Convert.ToInt32(
                                   CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                               : 0;
                }
            }

            n += Convert.ToInt32(BankInterestIncome);
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Bank Interest Income");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", BankInterestIncome);
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", n);
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sum += n;
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", sum);
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sb.AppendFormat("<tr><td><br /></td></tr>");
            int m = 0;
            int sum1 = 0;
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Distribution cost");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");
            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "1" && fun.Kinds == "3"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    sb.AppendFormat("   <tr>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", Detail.EnglishName);
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount.ToString() : "");
                    sb.AppendFormat("       </td>");
                    //sb.AppendFormat("       <td>");
                    //sb.AppendFormat("           {0}", CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount : "");
                    //sb.AppendFormat("       </td>");
                    sb.AppendFormat("   </tr>");

                    m += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                               ? Convert.ToInt32(
                                   CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                               : 0;
                    //sumold -= CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                    //          ? Convert.ToInt32(
                    //              CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                    //          : 0;
                }
            }
            sum1 += m;

            sb.AppendFormat("<tr><td><br /></td></tr>");
            m = 0;
            sb.AppendFormat("   </tr>");
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Administrative expenses");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");
            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "1" && fun.Kinds == "2"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    sb.AppendFormat("   <tr>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", Detail.EnglishName);
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount.ToString() : "");
                    sb.AppendFormat("       </td>");
                    //sb.AppendFormat("       <td>");
                    //sb.AppendFormat("           {0}", CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount : "");
                    //sb.AppendFormat("       </td>");
                    sb.AppendFormat("   </tr>"); ;

                    m += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                               ? Convert.ToInt32(
                                   CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                               : 0;
                    //sumold -= CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                    //          ? Convert.ToInt32(
                    //              CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                    //          : 0;
                }
            }
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           ");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", m);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sum1 += m;

            sb.AppendFormat("<tr><td><br /></td></tr>");

            m = 0;
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           Other operating expenses");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("          Bank charge");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", BankCharge);
            sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            m += Convert.ToInt32(BankCharge);
            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "1" && fun.Kinds == "1"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    sb.AppendFormat("   <tr>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", Detail.EnglishName);
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount.ToString() : "");
                    sb.AppendFormat("       </td>");
                    //sb.AppendFormat("       <td>");
                    //sb.AppendFormat("           {0}", CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount : "");
                    //sb.AppendFormat("       </td>");
                    sb.AppendFormat("   </tr>");


                    m += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                             ? Convert.ToInt32(
                                 CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                             : 0;
                    //sumold -= CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                    //          ? Convert.ToInt32(
                    //              CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                    //          : 0;
                }
            }

            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "5" && fun.Kinds == "1"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    sb.AppendFormat("   <tr>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", Detail.EnglishName);
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("       </td>");
                    sb.AppendFormat("       <td>");
                    sb.AppendFormat("           {0}", CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount.ToString() : "");
                    sb.AppendFormat("       </td>");
                    //sb.AppendFormat("       <td>");
                    //sb.AppendFormat("           {0}", CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ? CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount : "");
                    //sb.AppendFormat("       </td>");
                    sb.AppendFormat("   </tr>");

                    m += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                             ? Convert.ToInt32(
                                 CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                             : 0;
                }
            }

            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           ");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", m);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            sum1 += m;

            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           ");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", sum1);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");



            sb.AppendFormat("<tr><td><br /></td></tr>");
            sum -= sum1;
            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("          Loss  before taxation");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", sum);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");


            m = 0;
            foreach (var Detail in DetailTypesList.Where(fun => fun.TypeProviderID == "5" && fun.Kinds == "1"))
            {
                //if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0 ||
                //    CompanyDetailListOld.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0)
                if (CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId && fun.Amount != 0) > 0)
                {
                    m += CompanyDetailList.Count(fun => fun.DetailTypesId == Detail.TypeId) > 0
                             ? Convert.ToInt32(
                                 CompanyDetailList.First(fun => fun.DetailTypesId == Detail.TypeId).Amount)
                             : 0;
                }
            }

            int SumAll = sum;
            sum = m + sum;
            sum -= n;


            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("          Taxation");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", (-sum * 0.165).ToString("0"));//(sum * 0.165).ToString("0.00")
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", ProfitTaxOld);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");

            SumAll = (int)(SumAll + (-sum * 0.165));
            //sumold = (int)(sumold - ProfitTaxOld);

            sb.AppendFormat("   <tr>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("          Profit/(Loss) for the orderid/period");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("       </td>");
            sb.AppendFormat("       <td>");
            sb.AppendFormat("           {0}", SumAll);
            sb.AppendFormat("       </td>");
            //sb.AppendFormat("       <td>");
            //sb.AppendFormat("           {0}", sumold);
            //sb.AppendFormat("       </td>");
            sb.AppendFormat("   </tr>");




            sb.AppendFormat("</table>");

            sb.AppendFormat("The notes on pages {0} to {1} form part of these financial statements.", 0, 0);


            Label1.Text = sb.ToString();
        }
    }
}